package pacBioSimple;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import pacBiopackage.GetOpt;

public class MapListUniqueFilter {
	public static void main(String[] args) 
	{
		GetOpt go = new GetOpt(args, "o:a:");
		go.optErr = true;
		int ch = -1;
		
		// process options in command line arguments
		// Set default values
		boolean usagePrint = false;                 
		String fileInput = "";   
		String filenameOutput="output.txt";
		while ((ch = go.getopt()) != GetOpt.optEOF) 
		{
		    if ((char)ch == 'h') {
		       usagePrint = true;
		   }
		    else if ((char)ch == 'a') {
		    	fileInput = go.optArgGet();
		    }
		    else if((char)ch =='o')
		    {
		    	filenameOutput = go.optArgGet();
		    }
		    else
		        System.exit(1);                     // undefined option
		}                                           // getopt() returns '?'
		
		if (usagePrint || fileInput.isEmpty()) {
		    System.out.println("Usage: -a zipData [ -o outputFile -h]"); //NOI18N
		    System.exit(0);
		}
		
		// @TODO check if files exist
	
		System.out.println("------------------------");
		System.out.println("Command line parameter settings:");
		System.out.println("-a " + fileInput); //NOI18N
		System.out.println("-o " + filenameOutput); //NOI18N
		System.out.println("------------------------");
		String line="";
		try
		{
//			InputStream fileStream = new FileInputStream(fileInput);
//			InputStream gzipStream = new GZIPInputStream(fileStream);
//			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gzipStream));
			
			
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(fileInput))));
			
			PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(filenameOutput)))));
			while(bufferedReader.ready())
			{
				 line= bufferedReader.readLine();
				//System.out.println(line);
				StringTokenizer tk = new StringTokenizer(line);
				tk.nextToken();
				tk.nextToken();
				tk.nextToken();
				tk.nextToken();
				tk.nextToken();
				tk.nextToken();
				int unique = Integer.valueOf(tk.nextToken());
				if(unique==1)
				{
					pw.println(line);
				}				
			}
			pw.flush();
			pw.close();
			bufferedReader.close();
		}
		catch(Exception e)
		{
			System.out.println(line);
			e.printStackTrace();
		}
	}
}
